/* Copyright 2010 Michael Wenders, michaelwenders <at> gmx <dot> de

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "lcm_includes.h"

LcmConfig::LcmConfig(string filename)
{
	this->setFilename(filename);
}

void LcmConfig::setFilename(string filename)
{
	this->filename = filename;

}
string LcmConfig::getFilename()
{
	return this->filename;
}

void LcmConfig::setPort(int port)
{
  //FIXME check
  this->port = port;
}
int LcmConfig::getPort()
{
  return this->port;
}
int LcmConfig::checkPort(string port)
{
	//FIXME: check really
	int value;
	stringstream ss (stringstream::in | stringstream::out);

	ss << port;
	ss >> value;
	return value;
}

void LcmConfig::setIP(string ip)
{
	if(checkIP(ip) == OK)
	{
		this->ip = ip;
	}
}
string LcmConfig::getIP()
{
	return this->ip;
}
int LcmConfig::checkIP(string ip)
{
	//FIXME:
	return OK;
}

void LcmConfig::setConfigDir(string configDir)
{
  this->configDir = configDir;
}
string LcmConfig::getConfigDir()
{
  return this->configDir;
}

int LcmConfig::loadAll()
{
	if(setDefaults() != OK)
		return NOT_OK;

	if(loadFile() != OK)
		return NOT_OK;

	return OK;
}

int LcmConfig::loadFile()
{
	//check if file exists
	if(!lcm::doesFileExist(this->filename))
	{
		cout << "Could not find config file '" << filename << "' or permission denied" << endl;
		return NOT_OK;
	}

	// loading line by line
	ifstream file;
	file.open(filename.c_str(), ios::in);

	if(file)
	{
		string line;
		for(lineNumber = 1; !file.eof(); lineNumber++)
		{
			getline(file,line);

			//upper case to lower case
			line = lcm::lineToLowerCase(line);

			//erase comments & spaces
			line = lcm::eraseComments(line);
			line = lcm::eraseSpaces(line);

			//ignore empty lines
			if(line != "")
			{//get meta-option
				string meta_option = lcm::getMetaOption(line);
				if(meta_option == "core")
					{ this->parseCoreOptions(line); }
				else if (meta_option == "net")
					{ this->parseNetOptions(line); }
				else if (meta_option == "log");
			//		{ this->parse_log_line(line); }
				else
				{
					cout << "Invalid string '" << meta_option << "' at line " << lineNumber << endl;
				}
			}//ignore empty lines*/
		}//while (!file.eof())
		file.close();
	}
	return OK;
}

int LcmConfig::parseCoreOptions(string line)
{
	//line is already without any spaces, or '#'s
	string parameter = lcm::getParameter(line);

	if(parameter == "configdir")
	{
		this->setConfigDir(lcm::getValue(line));
	}
	else
		cout << "Unknown parameter '" << parameter << "' at line " << lineNumber << endl;
	return 0;
}
int LcmConfig::parseNetOptions(string line)
{
	//line is already without any spaces, or '#'s
	string parameter = lcm::getParameter(line);

	if(parameter == "ip")
	{
		this->setIP(lcm::getValue(line));
	}
	else if(parameter == "port")
	{
		this->setPort(this->checkPort(lcm::getValue(line)));
	}
	else
		cout << "Unknown parameter '" << parameter << "' at line " << lineNumber << endl;
	return 0;
}
int LcmConfig::parseLogOptions(string line)
{
	//line is already without any spaces, or '#'s
	string parameter = lcm::getParameter(line);

	if(parameter == "file")
	{

	}
	else if(parameter == "level")
	{

	}
	else
		cout << "Unknown parameter '" << parameter << "' at line " << lineNumber << endl;
	return 0;
}


int LcmConfig::setDefaults()
{

	this->setConfigDir("/etc/lcm");
	//this->setPort(1508);

	return OK;
}
